package com.qs.eggs.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;

public class SubmitGoodsAdapter extends RecyclerView.Adapter<SubmitGoodsAdapter.MyViewHolder>{
    private Context mContext;
    private  String imageUrl;
    private float allPrice=0;
    ArrayList<String> count=new ArrayList<>();
    String [] temp;
    ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();

    public interface OnItemClickLitener
    {
        void onItemClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
    public SubmitGoodsAdapter(Context mContext, ArrayList<HashMap<String, Object>> listItem,String listCount){
        this.mContext=mContext;
        this.listItem=listItem;
        this.mContext = mContext;
        this.listItem = listItem;
        temp=listCount.split(",");
    }

    @Override
    public SubmitGoodsAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        SubmitGoodsAdapter.MyViewHolder holder = new SubmitGoodsAdapter.MyViewHolder(LayoutInflater.from(
                mContext).inflate(R.layout.item_dingdan, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final SubmitGoodsAdapter.MyViewHolder holder, final int position) {

        imageUrl= Constants.URL_BASE+"productimages/"+listItem.get(position).get("autoname");
        holder.proname.setText(listItem.get(position).get("proname").toString());
        holder.price.setText("¥"+listItem.get(position).get("price")+"");

        holder.count.setText("X"+getCount1().get(position)+"");

        Picasso.with(mContext).load(imageUrl).into(holder.logo);

        // 如果设置了回调，则设置点击事件
        if (mOnItemClickLitener != null)
        {
            holder.itemView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });
        }
    }

    @Override
    public int getItemCount()
    {
        return listItem.size();
    }

    public float getTotalPrice(){
        for(int i=0;i<temp.length;i++){
            allPrice=allPrice+Float.valueOf(listItem.get(i).get("price").toString())*Integer.parseInt(getCount1().get(i));

        }

        return allPrice;
    }
    private ArrayList<String> getCount1(){

        for(int i=0;i<temp.length;i++){
            count.add(temp[i]);
        }
        return count;
    }
    class MyViewHolder extends RecyclerView.ViewHolder
    {

        public TextView price,count,proname;
        public ImageView logo;
        public MyViewHolder(View view)
        {
            super(view);
            price = (TextView) view.findViewById(R.id.tv_jiage);
            count = (TextView) view.findViewById(R.id.num);
            proname = (TextView) view.findViewById(R.id.tv_name);
            logo=(ImageView)view.findViewById(R.id.imageView);

        }
    }
}

